package com.ray.activiti.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.ray.activiti.dto.ProcessTaskDto;
import com.ray.activiti.enums.RunType;
import com.ray.activiti.table.entity.ExtendActNodeset;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.xiaoleilu.hutool.util.StrUtil;

public class CallBackUtil {
	private  final static String condition = "condition";
 
    /**
     * 节点回调方法 执行
     * @param callBack
     * @param processTaskDto
     * @throws Exception
     */
    public static Object executeCallback(ExtendActNodeset node,ProcessTaskDto processTaskDto,String type) throws Exception {
    	    if(node != null){
    	    	if(StringUtils.isNotBlank(node.getRunBean())){
    	    		 String runBean  = node.getRunBean();
    	    		 int lastIndex = runBean.lastIndexOf(".");
    	             String methodStr = runBean.substring(lastIndex+1);//方法名
    	             String beanName = runBean.substring(0, lastIndex);//类路径
    	             return SpringBeanRunUtil.run(beanName,methodStr,processTaskDto,type);
    	            
    	    	}else if(StringUtils.isNotBlank(node.getRunUrl())){//http请求 待处理
    	    		return null;
    	    	}
    	    }
           return null;
    }
    
    /**
     * 节点条件回调方法 执行
     * @param callBack
     * @param processTaskDto
     * @throws Exception
     */
    public static Object executeCinditionCallback(ExtendActNodeset node,ProcessTaskDto processTaskDto,String type) throws Exception {
    	    if(node != null){
    	    	if(StringUtils.isNotBlank(node.getConditionBean())){
    	    		 String runBean  = node.getConditionBean();
    	    		 int lastIndex = runBean.lastIndexOf(".");
    	             String methodStr = runBean.substring(lastIndex+1);//方法名
    	             String beanName = runBean.substring(0, lastIndex);//类路径
    	             return SpringBeanRunUtil.run(beanName,methodStr,processTaskDto,type);
    	            
    	    	}else if(StringUtils.isNotBlank(node.getConditionUrl())){//http请求 待处理
    	    		return null;
    	    	}
    	    }
           return null;
    }

    /**
     * 节点驳回回调方法 执行
     * @param callBack
     * @param processTaskDto
     * @throws Exception
     */
    public static Object cancelExecuteCallback(ExtendActNodeset node,ProcessTaskDto processTaskDto,String type) throws Exception {
    	    if(node != null){
    	    	if(StringUtils.isNotBlank(node.getCancelBean())){
    	    		 String runBean  = node.getCancelBean();
    	    		 int lastIndex = runBean.lastIndexOf(".");
    	             String methodStr = runBean.substring(lastIndex+1);//方法名
    	             String beanName = runBean.substring(0, lastIndex);//类路径
    	             return SpringBeanRunUtil.run(beanName,methodStr,processTaskDto,type);
    	            
    	    	}else if(StringUtils.isNotBlank(node.getCancelUrl())){//http请求 待处理
    	    		return null;
    	    	}
    	    }
           return null;
    }
    
	@SuppressWarnings("unchecked")
	public static void setVariables(ExtendActNodeset nodeSet,ProcessTaskDto processTaskDto, Map<String, Object> variables) throws Exception {
		Object variable = executeCinditionCallback(nodeSet, processTaskDto, RunType.CONDITION.getType());
		if(ObjectUtil.isNotNull(variable)){
			if(variable instanceof String){
				variables.put(condition, (String)variable);
			}else if(variable instanceof Map){
				variables.putAll((Map<String,String>)variable);
			}if(variable instanceof HashMap){
				variables.putAll((HashMap<String,String>)variable);
			}
		}else if(StrUtil.isNotBlank(nodeSet.getDefaultValue())){
			variables.put(condition, nodeSet.getDefaultValue());
		}
	}
}
